﻿using Enums;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Model.Request;
using Model.Response;
using TemplateInterfaceService;
using Untils;

namespace Template_Api.Controllers
{
    /// <summary>
    /// 用户控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]//路由规则
    [ApiExplorerSettings(GroupName = nameof(ApiVersionInfo.v1))]//Swagger版本
    public class UserController : ControllerBase
    {
        private readonly IUserService _userService;//用户服务
        private readonly ILogger<UserController> _logger;//日志服务
        
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="userService"></param>
        public UserController(ILogger<UserController> logger, IUserService userService)
        {
            _logger = logger;
            _userService = userService;
        }

        /// <summary>
        /// 根据查询条件获取用户列表
        /// </summary>
        /// <param name="req">查询条件</param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public async Task<GetSearchUsersResponse> GetSearchUsers(GetSearchUsersRequest req)
        {                                                            
            try
            {
                return await _userService.GetSearchUsers(req);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, LogMessage.PROCESS_ACTION_ERROR, RouteData.Values[RoutData.controller], RouteData.Values[RoutData.action]);
                return ApiResponseHelper.ErrorMessage<GetSearchUsersBody, GetSearchUsersResponse>(SystemCode.SYSTEM_ERROR, ApiResponseHelper.GetDescription(SystemCode.SYSTEM_ERROR), null);
            }
        }
    }
}
